package com.tronsis.distribution.biz.impl;

import android.app.Activity;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tronsis.distribution.bean.ResourceBean;
import com.tronsis.distribution.biz.ResourceBiz;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.http.HttpCallBack;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.RequestClient;
import com.tronsis.library.http.RequestMethod;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.LogUtil;

public class ResourceBizImpl implements ResourceBiz {
    private static String TAG = ResourceBizImpl.class.getSimpleName();


    @Override
    public void getResource(final Activity activity, boolean onlyMime, int page, String search, int size, String typeId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<ResourceBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:getResource");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("onlyMime", String.valueOf(onlyMime), "page", String.valueOf(page), "search", search, "size", String.valueOf(size), "typeId", typeId).excute(RequestMethod.GET, Url.APP_RESOURCE);

    }

    @Override
    public void addResource(Activity activity, String content, String imageFid, String typeId, String videoFid, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:addResource");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("content", content, "imageFid", imageFid, "typeId", typeId, "videoFid", videoFid).excute(RequestMethod.POST, Url.APP_RESOURCE);
    }

    @Override
    public void updateResource(Activity activity, String resId, String content, String imageFid, String typeId, String videoFid, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:updateResource");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("content", content, "imageFid", imageFid, "typeId", typeId, "videoFid", videoFid).excute(RequestMethod.POST, Url.APP_RESOURCE + "/" + resId);

    }

    @Override
    public void deleteResource(Activity activity, String resId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:deleteResource");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.DELETE, Url.APP_RESOURCE + "/" + resId);

    }

    @Override
    public void getResourceType(Activity activity, boolean onlyMine, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<LinkedTreeMap<String, String>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:getResourceType");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("onlyMine", String.valueOf(onlyMine)).excute(RequestMethod.GET, Url.APP_RESOURCE_TYPES);

    }

    @Override
    public void addQuitProfit(Activity activity, String askId, String fid, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e(TAG, "请求成功:addQuitProfit");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("askId", askId, "fid", fid).excute(RequestMethod.POST, Url.UPGRADE_QUIT_SUBMIT);
    }
}
